﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace BSCore
{
    public class Tiles:List<Tile>
    {

        public Tiles GetTilesOrderedByZ()
        {
            Tiles result = new Tiles();
            DataTable dtOrdering = new DataTable();
            dtOrdering.Columns.Add(new DataColumn("X",typeof(float)));
            dtOrdering.Columns.Add(new DataColumn("Y",typeof(float)));
            dtOrdering.Columns.Add(new DataColumn("Z",typeof(float)));
            dtOrdering.Columns.Add(new DataColumn("ID", typeof(int)));
            
            foreach (Tile tile in this)
            {
                DataRow tmpdr = dtOrdering.NewRow();
                tmpdr["X"] = tile.Position.X;
                tmpdr["Y"] = tile.Position.Y;
                tmpdr["Z"] = tile.Position.Z;
                tmpdr["ID"] = tile.ID;
                dtOrdering.Rows.Add(tmpdr);
            }

            //TODO: Sort DataTable
            DataRow[] sortedShizzle = dtOrdering.Select("", "Z ASC");

            foreach (DataRow dr in sortedShizzle)
            {
                foreach (Tile tile in this)
                {
                    if (tile.ID == dr["ID"])
                    {
                        result.Add(tile);
                        break;
                    }
                }
            }

            return result;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Tiles t = GetTilesOrderedByZ();

            foreach (Tile tile in t)
            {
                tile.Draw(gameTime, spriteBatch);
            }
        }
    }
}
